/**
 * 
 */
package com.maohi.software.tupuna.home.bundle.ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.jdesktop.swingx.SwingXUtilities;
import org.jdesktop.swingx.border.DropShadowBorder;

import com.google.inject.Inject;
import com.maohi.software.arii.utils.Numbers;
import com.maohi.software.tupuna.interfaces.TupunaController;
import com.maohi.software.tupuna.ui.ImageButton;

/**
 * @author heifara
 * 
 */
public class HomeUI extends JPanel implements AncestorListener {

	protected class BundleUI extends JPanel implements ActionListener {

		private static final int BUNDLE_HEIGHT = 150;
		private static final int BUNDLE_WIDTH = 350;
		private static final int BUNDLE_BUTTON_HEIGHT = 100;
		private static final int BUNDLE_BUTTON_WIDTH = 100;
		private static final long serialVersionUID = 1L;
		private JButton bundleButton;
		private JButton menusButton;
		private JLabel titleLabel;
		private JTextArea descriptionLabel;

		private int index;
		private TupunaController controller;

		public BundleUI(int iIndex, TupunaController aController) {
			this.index = iIndex;
			this.controller = aController;
			this.updateUI();
		}

		@Override
		public void actionPerformed(ActionEvent aEvent) {
			if (aEvent.getSource().equals(this.getBundleButton())) {
				controller.getUi().openBundle(controller.getModel().getBundleId(this.index));
			} else if (aEvent.getSource().equals(this.getMenusButton())) {

			} else {

			}
		}

		public JButton getBundleButton() {
			if (this.bundleButton == null) {
				URL iUrl = controller.getModel().getBundleIconAt(this.index);
				if (iUrl != null) {
					this.bundleButton = new ImageButton(new ImageIcon(iUrl));
				} else {
					this.bundleButton = new JButton(controller.getModel().getBundleTitleAt(this.index));
				}

				this.bundleButton.setSize(new Dimension(BUNDLE_BUTTON_WIDTH, BUNDLE_BUTTON_HEIGHT));
				this.bundleButton.setPreferredSize(new Dimension(BUNDLE_BUTTON_WIDTH, BUNDLE_BUTTON_HEIGHT));
				this.bundleButton.setMinimumSize(new Dimension(BUNDLE_BUTTON_WIDTH, BUNDLE_BUTTON_HEIGHT));
				this.bundleButton.addActionListener(this);
			}
			return bundleButton;
		}

		public JTextArea getDescriptionLabel() {
			if (this.descriptionLabel == null) {
				this.descriptionLabel = new JTextArea(controller.getModel().getBundleDescriptionAt(this.index));
				this.descriptionLabel.setOpaque(false);
				this.descriptionLabel.setLineWrap(true);
				this.descriptionLabel.setBorder(BorderFactory.createEmptyBorder());
			}
			return descriptionLabel;
		}

		public JButton getMenusButton() {
			if (this.menusButton == null) {
				URL iUrl = getClass().getClassLoader().getResource("com.maohi.software.tupuna.icons/down-arrow-16x16.png");
				if (iUrl != null) {
					this.menusButton = new ImageButton(new ImageIcon(iUrl));
				} else {
					this.menusButton = new JButton("v");
				}
			}
			return menusButton;
		}

		public JLabel getTitleLabel() {
			if (this.titleLabel == null) {
				this.titleLabel = new JLabel(controller.getModel().getBundleTitleAt(this.index));
				this.titleLabel.setFont(new Font(this.titleLabel.getFont().getName(), Font.BOLD, 16));
			}
			return titleLabel;
		}

		@Override
		public void updateUI() {
			if (this.controller == null) {
				super.updateUI();
				return;
			}

			this.setSize(new Dimension(BUNDLE_WIDTH, BUNDLE_HEIGHT));
			this.setPreferredSize(new Dimension(BUNDLE_WIDTH, BUNDLE_HEIGHT));
			this.setMinimumSize(new Dimension(BUNDLE_WIDTH, BUNDLE_HEIGHT));
			this.setBorder(new DropShadowBorder(true));

			this.removeAll();
			this.setLayout(new GridBagLayout());
			GridBagConstraints iConstraints = new GridBagConstraints();
			iConstraints.insets = new Insets(5, 5, 5, 5);
			iConstraints.fill = GridBagConstraints.BOTH;

			iConstraints.gridy = 0;
			this.add(this.getTitleLabel(), iConstraints);

			iConstraints.gridy = 1;
			this.add(this.getBundleButton(), iConstraints);
			iConstraints.weightx = 1;
			iConstraints.weighty = 1;
			this.add(this.getDescriptionLabel(), iConstraints);
			iConstraints.weightx = 0;
			iConstraints.weighty = 0;

			iConstraints.gridy = 2;
			this.add(this.getMenusButton(), iConstraints);
			super.updateUI();
		}

	}

	private static final long serialVersionUID = 1L;
	private static final int TABBED_PANE_INDEX = 0;
	private TupunaController controller;
	private Icon icon;

	@Inject
	public HomeUI(TupunaController aController) {
		controller = aController;

		this.addAncestorListener(this);
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		JTabbedPane iTabbedPane = SwingXUtilities.getAncestor(JTabbedPane.class, this);
		if (iTabbedPane != null) {
			iTabbedPane.setTitleAt(TABBED_PANE_INDEX, "Home");
			iTabbedPane.setIconAt(TABBED_PANE_INDEX, this.icon);
		}
		this.update();
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
	}

	@Inject
	protected void initIcon(ClassLoader aClassLoader) {
		this.icon = new ImageIcon(aClassLoader.getResource("com.maohi.software.tupuna.icons/home32x32.png"));
	}

	protected void update() {
		this.removeAll();
		this.setLayout(new GridBagLayout());

		GridBagConstraints iConstraints = new GridBagConstraints();
		iConstraints.fill = GridBagConstraints.BOTH;
		iConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
		for (int iIndex = 0; iIndex < controller.getModel().getBundleCount(); iIndex++) {
			if (Numbers.isEven(iIndex)) {
				iConstraints.gridwidth = GridBagConstraints.RELATIVE;
			} else {
				iConstraints.gridwidth = GridBagConstraints.REMAINDER;
			}
			this.add(new BundleUI(iIndex, controller), iConstraints);
		}

		iConstraints.gridx = 0;
		iConstraints.gridwidth = GridBagConstraints.REMAINDER;
		iConstraints.weightx = 1;
		iConstraints.weighty = 1;
		this.add(new JLabel(), iConstraints);
	}
}
